﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Paso_out")]
    public class Paso_outController : ApiController
    { 
        [HttpPost]
        [Route("PostPaso_out_get_apply_bill_no")]
        public System.Data.DataSet PostPaso_out_get_apply_bill_no([FromBody]  JObject obj)
        { 
            string APPLY_BILL_NO = obj["APPLY_BILL_NO"] == null ? " " : obj["APPLY_BILL_NO"].ToString().Trim();
            string USERID = obj["USERID"] == null ? " " : obj["USERID"].ToString().Trim();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString().Trim();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString().Trim();

            //string sql_str1 = " select ASMAT_NO,ASMAT_NAME,SPEC,UNIT,GET_NUM, GET_NUM  GET_NUM1,PA_CON_FAC_RT,ASSIST_UNIT,GET_ASSIST_NUM,GET_ASSIST_NUM GET_ASSIST_NUM1 ,0 REPLACE_NUM  from  TPASO04 ta  ,tpaso03  tb  ";
            //sql_str1 += "  where ta.apply_bill_no = '" + apply_bill_no + "'  and ta.apply_bill_no = tb.apply_bill_no ";
            //sql_str1 += "  and tb.apply_status = '30' ";
            // EI.EIInfo outBlock = EF.Utility.ExecQuery(sql_str1); 





            EI.EIInfo inBlock = new EI.EIInfo();
            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.company_name = "龙腾特钢";
            inBlock.sys_info.userid = USERID;
            inBlock.sys_info.username = USERNAME;
            inBlock.sys_info.formname = FORMNAME;


          

            inBlock.Tables[0].Columns.Add("APPLY_BILL_NO");
            
            
            inBlock.Tables[0].Rows.Add();
            inBlock.Tables[0].Rows[0]["APPLY_BILL_NO"] = APPLY_BILL_NO;
           //inBlock.Tables[0].Rows[0]["ASMAT_USE_TYPE"] = "08";


            


            EI.EIInfo outBlock = EI.EITuxedo.CallService("paso01_inq2", inBlock);
           

            


         
            outBlock.Tables[0].TableName = "Table0";
             
            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;
        }
         
        

        //入库时，显示该授权下的仓库
        [HttpPost]
        [Route("PostPaso_out_Load")]
        public System.Data.DataSet PostPaso_out_Load([FromBody]  JObject obj)
        { 
            string userid   = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string pro_type = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString(); 



            string sql_str1 =  " select  t.STOCK_NO CODE ";
            sql_str1 += " ,t.stock_name   CODE_DESC_1_CONTENT  ,t.COMPANY_CODE ,t.BRANCH_CODE ";
            sql_str1 += "   from tsimm01 t,tsimm04 tt  ";
            sql_str1 += "    WHERE t.stock_no = tt.stock_no ";
            sql_str1 += "   and tt.staff_id = '" + userid + "' ";

            if (pro_type =="P2")
            {
                sql_str1 += "   and  t.STOCK_TYPE='A'"; 
            }
            else if (pro_type == "P1")
            {
                sql_str1 += "   and  t.STOCK_TYPE='7'";
            }
            else //
            {
                sql_str1 += "   and  t.STOCK_TYPE='A'"; 
            }
           






            EI.EIInfo outBlock = EF.Utility.ExecQuery(sql_str1); 

            outBlock.Tables[0].TableName = "STOCK_NO";


            //入库类别
            EI.EIInfo outBlock2 = EF.Utility.GetCodeClassValue("DM41");

            //DataRow dr4 = outBlock.Tables["FIBR"].NewRow();
            //dr4["CODE"] = " ";
            //dr4["CODE_DESC_1_CONTENT"] = " ";
            //outBlock.Tables["FIBR"].Rows.InsertAt(dr4, 0);
            outBlock2.Tables[0].TableName = "DM41";


            outBlock.Tables.Add(outBlock2.Tables[0].Copy());


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        //入库，入库实绩新增 + 入库确认。[json表结构体参数相互传送]
        [HttpPost]
        [Route("PostPaso_out_save")] 
        
        public System.Data.DataSet PostPaso_out_save([FromBody]  JObject obj)
        {
            string APPLY_BILL_NO = obj["APPLY_BILL_NO"] == null ? " " : obj["APPLY_BILL_NO"].ToString().Trim();
           
            string USERID = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();
             
            DataTable table_rec = JsonConvert.DeserializeObject<DataTable>(obj["table_rec"].ToString(), new DingApi.ReadAheadDataTableConverter());;


            if (!(table_rec.Columns.Contains("STATE_JYF")))
                table_rec.Columns.Add("STATE_JYF");

            for (int i = 0; i < table_rec.Rows.Count; i++)
            {
                table_rec.Rows[i]["STATE_JYF"] = "add";
            }


            EI.EIInfo inBlock = new EI.EIInfo();
            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.company_name = "常熟市龙腾特种钢有限公司";

            inBlock.sys_info.userid = USERID;
            inBlock.sys_info.username = USERNAME;
            inBlock.sys_info.formname = FORMNAME;

            //0 为主项；
            inBlock.AddNewBlock();//1为删除操作
            inBlock.AddNewBlock();//2为本次新增明细
            inBlock.AddNewBlock();//3账套标识


          


            //主项中有用的语句均在此

            string sql_str0 = "select PA_STOCK_NO,APPLY_FLAG,  '10'   BILL_STATUS, 'O' IOSTOCK_TYPE , sysdate   OUTSTOCK_DATE,  ";
            sql_str0 += "  TX.DEPT_CODE,  0   OUTSTOCK_AMT,TX.ASMAT_USE_TYPE,TX.STOCK_NO  ";
            sql_str0 += " ,TX.APPLY_BILL_NO,TX.ARCHIVE_FLAG ,TX.DPTMENT_CODE ,COMPANY_CODE ,USER_CODE from tpaso03  TX ";
            sql_str0 += " WHERE apply_bill_no = '" + APPLY_BILL_NO + "' ";

            EI.EIInfo inInfo = EF.Utility.ExecQuery(sql_str0);



            //主项
            inBlock.Tables[0].Merge(inInfo.Tables[0]);

            //删除 inBlock.Tables[1]

            //新增
            inBlock.Tables[2].Merge(table_rec.Select(" QUANTITY  >0 ").CopyToDataTable<DataRow>());

            //3账套标识
            inBlock.Tables[3].Columns.Add("S_COMPANY_CODE");
            inBlock.Tables[3].Rows.Add(inInfo.Tables[0].Rows[0]["COMPANY_CODE"].ToString());






            string apply_bill_no = "";
            EI.EIInfo outBlock = EI.EITuxedo.CallService("paso0102_sav", inBlock);
            if (outBlock.sys_info.flag < 0)
            {
                
            }
            else
            {
                EI.EIInfo inInfo2 = new EI.EIInfo();
                inInfo2.Tables[0].Merge(outBlock.Tables["TPASO01"]);

                inInfo2.sys_info.company_code = "LT";
                inInfo2.sys_info.company_name = "常熟市龙腾特种钢有限公司";
                inInfo2.sys_info.userid = USERID;
                inInfo2.sys_info.username = USERNAME;

                EI.EIInfo outInfo2 = EI.EITuxedo.CallService("paso0102_adt", inInfo2);

                if (outInfo2.sys_info.flag < 0)
                {
                    outBlock.sys_info.flag = outBlock.sys_info.flag;
                    outBlock.sys_info.msg = outBlock.sys_info.msg+"";
                }
                apply_bill_no = inInfo2.Tables[0].Rows[0]["IOSTOCK_BILL_NO"].ToString();
            }

                   
   
            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Columns.Add("APPLY_BILL_NO");

            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg, apply_bill_no);
            return outBlock;

        }

    }
}